﻿Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public NotInheritable Class NativeMethods

    Private Sub New()
    End Sub

    Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Auto Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As WindowShowStyle) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    Private Declare Auto Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    Private Declare Auto Function GetWindowInfo Lib "user32" (ByVal hwnd As IntPtr, ByRef pwi As WINDOWINFO) As Boolean

    Shared ReadOnly SWP_NOMOVE As UInt32 = Convert.ToUInt32(&H2)
    Shared ReadOnly SWP_NOZORDER As UInt32 = Convert.ToUInt32(&H4)

    Private Enum WindowShowStyle As UInteger
        Hide = 0
        ShowNormal = 1
        ShowMinimized = 2
        ShowMaximized = 3
        Maximize = 3
        ShowNormalNoActivate = 4
        Show = 5
        Minimize = 6
        ShowMinNoActivate = 7
        ShowNoActivate = 8
        Restore = 9
        ShowDefault = 10
        ForceMinimized = 11
    End Enum

    <StructLayoutAttribute(LayoutKind.Sequential)> _
    Private Structure WINDOWINFO
        Public cbSize As UInteger
        Public rcWindow As RECT
        Public rcClient As RECT
        Public dwStyle As UInteger
        Public dwExStyle As UInteger
        Public dwWindowStatus As UInteger
        Public cxWindowBorders As UInteger
        Public cyWindowBorders As UInteger
        Public atomWindowType As UShort
        Public wCreatorVersion As UShort
    End Structure

    <StructLayoutAttribute(LayoutKind.Sequential)> _
    Private Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
    End Structure


    Public Shared Sub HideWindow(ByVal windowName As String)
        Dim handle As IntPtr = Nothing
        Dim counter As Integer
        While (handle = Nothing) And (counter < 5)
            System.Threading.Thread.Sleep(500)
            handle = FindWindow(Nothing, windowName)
            counter += 1
        End While
        If counter < 5 Then
            ShowWindow(handle, WindowShowStyle.Hide)
        End If
    End Sub

    Public Shared Sub ResizeWindow(ByVal className As String, ByVal width As Integer, ByVal height As Integer)
        Dim handle As IntPtr = Nothing
        Dim counter As Integer
        While (handle = Nothing) And (counter < 5)
            System.Threading.Thread.Sleep(500)
            handle = FindWindow(className, Nothing)
            counter += 1
        End While
        If counter < 5 Then
            Dim borderSize As Drawing.Size = GetBorderSize("pSX")
            width += borderSize.Width
            height += borderSize.Height
            SetWindowPos(handle, Nothing, 0, 0, width, height, SWP_NOMOVE Or SWP_NOZORDER)
        End If
    End Sub

    <CLSCompliant(False)> _
    Public Shared Function GetBorderSize(ByVal className As String) As Drawing.Size
        Dim info As New WINDOWINFO
        info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info))
        Dim handle As IntPtr = Nothing
        Dim counter As Integer
        While (handle = Nothing) And (counter < 5)
            System.Threading.Thread.Sleep(500)
            handle = FindWindow(className, Nothing)
            counter += 1
        End While
        If counter < 5 Then
            GetWindowInfo(handle, info)
            Dim windowSize As New Drawing.Size(info.rcWindow.right - info.rcWindow.left, info.rcWindow.bottom - info.rcWindow.top)
            Dim clientSize As New Drawing.Size(info.rcClient.right - info.rcClient.left, info.rcClient.bottom - info.rcClient.top)
            Dim borderSize As New Drawing.Size(windowSize.Width - clientSize.Width, windowSize.Height - clientSize.Height)
            Return borderSize
        End If
        Return Nothing
    End Function

End Class
